﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) [INCLUDE APACHE 2.0 AS AN OPTION.]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ppscd
{
    public class UrlUtils
    {
        public static readonly char DirectorySeparatorChar = '/';

        public static string Combine(params string[] paths)
        {
            StringBuilder url = new StringBuilder();

            for (int i = 0; i < paths.Length; i++)
            {
                if (i == 0)
                {
                    url.Append(paths[i]);
                    continue;
                }

                char urlLastChar = url[url.Length - 1];
                char pathFirstChar = paths[i][0];

                if (urlLastChar != DirectorySeparatorChar && pathFirstChar != DirectorySeparatorChar)
                {
                    url.Append(DirectorySeparatorChar);
                }

                if (urlLastChar == DirectorySeparatorChar && pathFirstChar == DirectorySeparatorChar)
                {
                    url.Append(paths[i].TrimStart(DirectorySeparatorChar));
                }
                else
                {
                    url.Append(paths[i]);
                }
            }

            return url.ToString();
        }
    }
}
